package org.thinkinbunny.financial.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {
	/**
	 * 检查EMAIL地址 用户名和网站名称必须>=1位字符
	 * */
	public static boolean checkEmailWithSuffix(String email) {
		String regex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";

		return startCheck(regex, email);
	}
	/**
	 * 检验用户名 取值范围为a-z,A-Z,0-9,"_""-",汉字， 用户名有最小长度和最大长度限制，比如用户名必须是4-20位
	 * */
	public static boolean checkUsername(String username, int min, int max) {
		String regex = "[\\w\u4e00-\u9fa5\\-a-zA-Z0-9_]{" + min + "," + max + "}";
		return startCheck(regex, username);
	}
	/**
	 * 检验密码 取值范围为a-z,A-Z,0-9,"_","-"，不能以"_"结尾 用户名有最小长度和最大长度限制，比如密码必须是6-20位 //
	 *
	 * @param password
	 * @param min
	 * @param max
	 * @return
	 */
	public static boolean checkPassword(String password, int min, int max) {

		String regex = "[a-zA-Z_0-9\\-]{" + min + "," + max + "}";
		return startCheck(regex, password);
	}

	public static boolean startCheck(String reg, String string) {
		boolean tem = false;

		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(string);

		tem = matcher.matches();
		return tem;
	}

}
